package com.twu.biblioteca;

import Helper.Utility;

import java.util.*;

public class BibliotecaApp {

    private String welcomeMessage = "welcome";
    private ArrayList<Book> books = new ArrayList<>();
    private ArrayList<Menu> menus = new ArrayList<>();
    private Scanner scanner = new Scanner(System.in);
    private Utility utility = new Utility();
    private String successfulCheckoutMessage = "Thank You! Enjoy the Book";
    private String unSuccessfulCheckoutMessage = "That book is not available";
    private String invalidOptionMessage = "select a vaild Opttion!";
    private String successfulReturnMessage = "Thank you for returning the book";
    private String unSuccessfulReturnMessage = "That is not a valid book to return";

    public String getInvalidOptionMessage() {
        return invalidOptionMessage;
    }

    public void setSuccessfulCheckoutMessage(String successfulCheckoutMessage) {
        this.successfulCheckoutMessage = successfulCheckoutMessage;
    }

    public String getSuccessfulCheckoutMessage() {
        return successfulCheckoutMessage;
    }

    protected void setWelcomeMessage(String newMessage) {
        welcomeMessage = newMessage;
    }

    protected String getWelcomeMessage() {
        return welcomeMessage;
    }


    public String welcome() {
        System.out.println(welcomeMessage);
        return welcomeMessage;
    }

    public void addBook(Book newBook) {
        Book bookAvailable = getAvailableBookByName(newBook.getTitle());
        if (bookAvailable != null)
            bookAvailable.addCopy();
        else
            books.add(newBook);
    }

    public void addMenu(Menu newMenu) {
        menus.add(newMenu);
    }

    public String listBooks() {
        return utility.listToString(books, ',');
    }

    public String showMainMenu() {
        return utility.listToString(menus, '\n');
    }

    public static void main(String[] args) {
        new BibliotecaApp().startSystem();
    }

    public void startSystem() {
        initialize();
        welcome();
        String input = null;
        do {
            System.out.println("Enter your choice: ");
            System.out.println(showMainMenu());
            input = scanner.next().trim();
            doAction(input);
        } while (!input.equalsIgnoreCase("q"));
    }

    private void initialize() {
        utility.populateMenu(this);
        utility.populateBooks(this);
    }

    public String doAction(String key) {
        if (key.equalsIgnoreCase("l")) {
            System.out.println(listBooks());
            return "Success";
        }
        if (key.equalsIgnoreCase("c")) {
//            checkout book
            System.out.println("Enter name of the book to checkout:");
            System.out.println(checkOutByTitle(scanner.next()));
        }
        if (key.equalsIgnoreCase("q;")) {
            return "Exit";
        }
        if (key.equalsIgnoreCase("r")) {
            System.out.println("Enter the Title of the book to return");
            String returningBookTitle = scanner.next();
            System.out.println(returnBook(getAvailableBookByName(returningBookTitle)));
        }
        return invalidOptionMessage;
    }


    public String checkOutByTitle(String bookName) {
        Book bookToCheckout = getAvailableBookByName(bookName);
        if (bookToCheckout != null) {
            bookToCheckout.checkOut();
            return successfulCheckoutMessage;
        }
        return unSuccessfulCheckoutMessage;
    }

    public Book getAvailableBookByName(String bookName) {
        Iterator<Book> bookList = books.iterator();
        while (bookList.hasNext()) {
            Book book = bookList.next();
            if (book.getTitle().equalsIgnoreCase(bookName) && (book.getAvailableCopies() > 0)) {
                return book;
            }
        }
        return null;
    }

    public boolean isAvailable(Book book) {
        return getAvailableBookByName(book.getTitle()) != null;
    }

    public String returnBook(Book book) {
        if (book.isLended()) {
            if (book.checkIn()) {
                return successfulReturnMessage;
            }
        }
        return unSuccessfulReturnMessage;

    }

    public int getBooksCountInLibrary() {
        return books.size();
    }
}
